import React, { useRef, useState } from 'react';
import { connect } from 'umi';
import TableDataBlock from '@/components/TableDataBlock';
import Drawer from './components/Drawer'
const wechatEdit = (props) => {
  const { loading, dispatch, list } = props;
  const childRef = useRef();
  const [visible, setVisible] = useState(false);
  console.log(props)
  // 搜索参数
  const searchItems = [
    {
      label: '状态',
      name: 'status',
      type: 'select',
      select: {
        0: '禁用',
        1: '启用',
      },
    },
  ];

  // table 表头
  const getColumns = [
    {
      title: '页面名称',
      dataIndex: 'pageName',
    },

    {
      title: '创建人',
      dataIndex: 'organizationAdminName',
    },
    {
      type: 'handle',
      width: 200,
      dataIndex: 'id',
      render: (val, record) => {
        return [
          {
            auth: true,
            title: '编辑',
            click: () => {
              dispatch({
                type: 'wechatPage/fetchPageConfigDetail',
                payload: {
                  id: val,
                  pageType: "normal"
                },
                callback: (e = {}) => {
                  const { id, pageConfig = '{}' } = e
                  setVisible({
                    show: true,
                    type: 'normal',
                    detail: {
                      ...JSON.parse(pageConfig),
                      id
                    }
                  })
                }
              })

            },
          },
        ]
      }
    },
  ];
  const handleCallback = () => {
    childRef.current.fetchGetData();
  };
  const handleSaveRole = (detail = {}) => {
    setVisible(() => {
      return {
        show: true,
        detail,
        type: 'normal'
      };
    });
  };
  const extraBtn = [
    {
      text: '新增',
      onClick: () => {
        handleSaveRole();
      },
    },
  ];

  return (
    <>
      <TableDataBlock
        cardProps={{ title: '页面管理' }}
        btnExtra={extraBtn}
        cRef={childRef}
        loading={loading}
        params={{ pageType: "normal" }}
        searchItems={searchItems}
        columns={getColumns}
        rowKey={(record) => `${record.id}`}
        dispatchType="wechatPage/fetchPageConfig"
        {...list}
      ></TableDataBlock>
      <Drawer handleCallback={handleCallback} visible={visible} onClose={() => setVisible(false)}></Drawer>
    </>
  );
};

export default connect(({ wechatPage, loading }) => ({
  list: wechatPage.pageConfigList,
  loading: loading.models.wechatPage,
}))(wechatEdit);
